package com.example.cms.controller;



import com.example.cms.em.ErrorMesgEnum;
import com.example.cms.entity.LoginLog;
import com.example.cms.exception.BusinessException;
import com.example.cms.pojo.BaseModel;
import com.example.cms.ras.RSAJS;
import com.example.cms.service.LoginService;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Enzo
 * @Description TODO
 * @date 2018-11-12 15:39
 */
@RestController
@RequestMapping("index")
public class LoginController extends BaseController {
    @Autowired
    LoginService loginService;
//    @Autowired
//    SysMenuService menuService;

//    @Autowired
//    SysRoleService roleService;
    /**
     * @return com.dc.base.pojo.BaseModel
     * @title:<h3> 用户登录 <h3>
     * @author: Enzo
     * @date: 2018-11-12 15:56
     * @params [loginLog, baseModel]
     **/

    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_account", value = "帐号"
                    , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "user_password", value = "密码"
                    , dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public BaseModel login(LoginLog loginLog, BaseModel baseModel) throws Exception {

        if (loginLog == null) {
            throw new BusinessException(ErrorMesgEnum.NO_LOGIN.LOGIN_NULL_USER);
        } else if (loginLog.getUser_account() == null || "".equals(loginLog.getUser_account()) || loginLog.getUser_password() == null || "".equals(loginLog.getUser_password())) {
            throw new BusinessException(ErrorMesgEnum.NO_LOGIN.LOGIN_NULL_USER);
        } else {
            loginLog.setType(0);//标志pc登录
            loginLog.setRequest_ip(request.getRemoteAddr());
            loginLog.setVersion_num(request.getHeader("User-Agent"));
            loginLog.setSession_id(session.getId());
            //解密
            try {
                String password = RSAJS.sessionDecryptHexStr(session, loginLog.getUser_password());
                loginLog.setUser_password(password);
            } catch (Exception e) {
                throw new BusinessException(ErrorMesgEnum.LOGIN_PASSWORD);
            }
            loginService.login(loginLog, baseModel);
            //if(baseModel.getResultCode()>0){
            //  throw  new BusinessException(baseModel.getResultCode(),baseModel.getMessage());
            //}
            session.setAttribute("user", baseModel.getData());
//            session.setAttribute(BaseContants.LOGIN_PERMISSION,roleService.selectMergeRolePermissionByUserId(loginLog.getUser_id()));
        }
        return baseModel;
    }

    /**
     * @return com.dc.base.pojo.BaseModel
     * @title:<h3> 查询权限菜单枚举类 <h3>
     * @author: Enzo
     * @date: 2018-11-14 11:02
     * @params [baseModel]
     **/
//    @ApiOperation(value = "查询权限菜单枚举类", httpMethod = "GET")
//    @RequestMapping("selectEnumRoleMenu")
//    public BaseModel selectEnumRoleMenu(BaseModel baseModel) throws Exception {
//        loginService.selectEnumRoleMenu(baseModel);
//        baseModel.setMessage("查询权限菜单成功");
//        return baseModel;
//    }
    /**
     * @title:<h3> 刷新权限菜单 <h3>
     * @author: Enzo
     * @date: 2018-11-14 14:23
     * @params [baseModel]
     * @return com.dc.base.pojo.BaseModel
     **/
//    @ApiOperation(value = "刷新权限菜单",httpMethod = "GET")
//    @RequestMapping("refreshSysMenu")
//    public BaseModel refreshSysMenu(BaseModel baseModel)throws Exception{
//        menuService.refreshSysMenu(baseModel);
//        baseModel.setMessage("刷新权限菜单成功");
//        return baseModel;
//    }
//    @ApiOperation(value = "查询当前用户权限菜单",httpMethod = "GET" ,response = SysMenu.class)
//    @RequestMapping("/findMyMenu")
//    public BaseModel findMyMenu(BaseModel baseModel) throws Exception {
//        //查询角色权限，根据用户id
//        Map<String, String> map = (Map<String, String>) session.getAttribute(BaseContants.LOGIN_PERMISSION);
//        if (map == null) {
//            throw new BusinessException(ErrorMesgEnum.NO_LOGIN);
//        }
//        menuService.findMyMenu(map, baseModel);
//        baseModel.setMessage("查询权限菜单成功");
//        return baseModel;
//    }
    @ApiOperation(value = "注销",httpMethod = "POST")
    @RequestMapping("/LoginOut")
    public BaseModel LoginOut(@ModelAttribute LoginLog sysLoginLog, BaseModel baseModel)throws  Exception{
        sysLoginLog.setSession_id(session.getId());
        sysLoginLog.setLoginout_type(1);
        loginService.Loginout(sysLoginLog,baseModel);
        session.invalidate();
        baseModel.setMessage("注销成功");
    return baseModel;
    }
}
